﻿Shader "Custom/Glow"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        [NoScaleOffset]_GlowTex ("光效图", 2D) = "black" {}
        _ScreenUVRotation("光效旋转", Float) = 0
        _ScreenUVTiling("光效缩放与偏移", Vector) = (1, 1, 0, 0)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _GlowTex;

        struct Input
        {
            float2 uv_MainTex;
            float3 worldPos;
        };

        half _Glossiness;
        half _Metallic;
        half _ScreenUVRotation;
        half4 _ScreenUVTiling;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        // 旋转矩阵
        float4x4 Rotation(float4 rotation)
        {
	        float radX = radians(rotation.x);
	        float radY = radians(rotation.y);
	        float radZ = radians(rotation.z);

	        float sinX = sin(radX);
	        float cosX = cos(radX);

	        float sinY = sin(radY);
	        float cosY = cos(radY);

	        float sinZ = sin(radZ);
	        float cosZ = cos(radZ);

	        return float4x4(cosY * cosZ, -cosY * sinZ, sinY, 0.0,
		        cosX * sinZ + sinX * sinY * cosZ, cosX * cosZ - sinX * sinY * sinZ, -sinX * cosY, 0.0,
		        sinX * sinZ - cosX * sinY * cosZ, sinX * cosZ + cosX * sinY * sinZ, cosX * cosY, 0.0,
		        0.0, 0.0, 0.0, 1.0);
        }

        inline half2 RotationUV(half rotation, half2 UV)
        {
            return mul(Rotation(half4(0, rotation, 0, 0)), half3(UV.x, 0, UV.y)).xz;
        }

        inline half2 RotationUV(half rotation, half2 UV, half4 Tiling)
        {
            half2 uv = UV * Tiling.xy - Tiling.xy * 0.5;
            uv = RotationUV(rotation, uv);
            uv += Tiling.xy * 0.5 + Tiling.zw;
            return uv;
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            half4 ScreenPos = ComputeNonStereoScreenPos(UnityWorldToClipPos(IN.worldPos));
            half2 ScreenUV = ScreenPos.xy / ScreenPos.w;

            _ScreenUVTiling.zw = _ScreenUVTiling.zw + _Time.y * _ScreenUVTiling.zw;
            ScreenUV = RotationUV(_ScreenUVRotation, ScreenUV, _ScreenUVTiling);

            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            c.rgb = tex2D(_GlowTex, ScreenUV).rgb + c.rgb;

            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
